<?php
	namespace app\service\admin;
	use core\BaseService;
	use app\model\User;
	use core\Util;
	use app\model\Menu;
	use app\model\RoleMenu;
	use app\model\RoleUser;
	use core\App;
	use app\model\Article;
	class UserService extends BaseService{
		function model(){
			return User::$tableName;
		}
		
		function saveUser($params,$info){
			if($params['id']){
				$params = User::filter($params);
				$params["edit_time"] = Util::time();
				$this->update($params);
				$info->success =true;
				$info->msg = '操作成功，数据已更新！';
			}else{
				$params = User::filter($params);
				$time = Util::time();
				$params["create_time"] = $time;
				$params["edit_time"] = $time;
				$this->insert($params);
				$info->success =true;
				$info->msg = '操作成功，数据已保存！';
			}
		}
		
		function deleteById($userIds,$info){
			$uidArr = explode(',',$userIds);
			//删除用户
			$count = $this->table()->whereIn('id',$uidArr)->delete();
			//删除用户的角色
			$this->table(RoleUser::$tableName)->whereIn('user_id',$uidArr)->delete();
			//删除用户发表的文章
			$this->table(Article::$tableName)->whereIn('user_id',$uidArr)->delete();
			$info->success = true;
			$info->msg = "操作成功，已删除".$count."条数据！";
		}
		
		function findUserNoMenusByUserId($userId){
			$m = Menu::$tableName;
			$rm = RoleMenu::$tableName;
			$ru = RoleUser::$tableName;
			$list = App::DB()->select('select m.key,m.url from '.$m.' as m where m.id not in(
select rm.menu_id from '.$rm.' as rm,'.$ru.' as ru where rm.role_id = ru.role_id and ru.user_id='.$userId.')');	
			return $list;
		}
	}